
import 'package:flutter/material.dart';
import 'package:osc/common/MyTextStyle.dart';
import 'package:osc/data/repository/repository.dart';
import 'package:osc/models/entity/osc_topic_detail_entity.dart';
import 'package:osc/res/dimens.dart';
import 'package:osc/ui/widgets/common_tab_bar.dart';
import 'package:osc/utils/pd_util.dart';

import 'topic_tweet_list.dart';

///话题动弹
class TopicTweetPage extends StatefulWidget {
  final int topicId;
  final String topicName;

  /// type 1:最新|2:最热
  const TopicTweetPage({Key key, this.topicId = 0, this.topicName}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return new TopicTweetPageState(topicId, topicName);
  }
}

class TopicTweetPageState extends State<TopicTweetPage>  with SingleTickerProviderStateMixin<TopicTweetPage>{
  final int topicId;
  final String topicName;
  TopicTweetPageState(this.topicId, this.topicName);


  double paddingParentHorizontal = Dimens.gap_activity_horizontal;
  double paddingChildHorizontal = Dimens.gap_view_normal;
  double paddingChildVertical = Dimens.gap_view_normal;

  double portraitSize = Dimens.view_item_icon_height_biggest;

  double expandedHeight = 160;

  TabController tabController;

  HttpRepository httpRepository;
  int joinCount = 0;
  String desc = "";


  @override
  void initState() {
    super.initState();
    tabController = TabController(length: 2, vsync: this);
    httpRepository = HttpRepository();
    getTopicDetail();
  }

  @override
  void dispose() {
    super.dispose();
    tabController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: buildNestedScrollView(),
    );
  }

  buildNestedScrollView(){
    return NestedScrollView(
      headerSliverBuilder: (BuildContext context, bool inBox){
        return [
          buildSliverAppBar(context),
        ];
      },
//      body: buildTabBarViewContent(),
      body: buildTabBarView(),
    );
  }

  buildSliverAppBar(BuildContext context){
    return SliverAppBar(
//      leading: null,
      title: Text(topicName),
      centerTitle: true,
      primary: true,//是否预留高度,ture时在状态栏下，false 最上面
      forceElevated: false,//是否显示阴影
      automaticallyImplyLeading: true,
      snap: false,//折叠效果，与floating结合使用，如果为true则floating必须为true,就是往下滑一点整个大背景就显示全部，往上滑动整个导航内容就隐藏
      floating: false,//是否随滑动隐藏标题,则滑动到最上面再滑动是否隐藏标题，true隐藏
      expandedHeight: expandedHeight+Dimens.gap_divider_height_smallest,//展开高度，默认是状态栏加标题的高度
      pinned: true,//是否固定导航在顶部，true 固定，false不固定，再上滑导航隐藏
      flexibleSpace: FlexibleSpaceBar(//展开区域
        centerTitle: false,
        title: Text(""),
        collapseMode: CollapseMode.pin,//背景 固定到位，直到达最小范围，默认 parallax（将以视差方式滚动）
        background: Container(
          alignment: Alignment.center,
          decoration: BoxDecoration(
            image: DecorationImage(
                image: AssetImage(PdUtil.getImagePath("bg_topic_5", png:false, jpg: true)),
              fit: BoxFit.fill,
              repeat: ImageRepeat.repeatY,
            ),
          ),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Divider(color: Colors.transparent,),
              Text("共有 $joinCount 人参与", style: MyTextStyle.TOPIC_JOIN_COUNT_TEXT,),
              Text(desc, style: MyTextStyle.TOPIC_DESC_TEXT,),
            ],
          ),
        ),
      ),
      bottom: buildTabBar(),
    );
  }


  buildComment(){
    return CommonTabBar(
        tabs: [
          Tab(text:"最热"),
          Tab(text:"最新"),
        ],
        pages: [
          TopicTweetList(topicId: topicId, topicName: topicName, type: 2,),
          TopicTweetList(topicId: topicId, topicName: topicName, type: 1,),
        ],
    );
  }

  buildTabBarViewContent(){
    return Column(
      children: <Widget>[
        buildTabBar(),
        Expanded(
          child: buildTabBarView(),
        )
      ],
    );
  }

  buildTabBar(){
    return TabBar(
      controller: tabController,
      tabs: [
        Tab(text:"最热"),
        Tab(text:"最新"),
      ],
    );
  }
  buildTabBarView(){
    return TabBarView(
      controller: tabController,
      children: [
        TopicTweetList(topicId: topicId, topicName: topicName, type: 2,),
        TopicTweetList(topicId: topicId, topicName: topicName, type: 1,),
      ],
    );
  }

  getTopicDetail() async{
    await httpRepository.getTopicDetail(topicId: topicId, name: topicName).then((resp){
      if(resp.success){
        OscTopicDetailEntity entity = resp.result;
        if(entity != null){
          joinCount = entity.joinCount;
          desc = entity.desc??"";
        }
      }
    });
    if(mounted){
      setState(() {

      });
    }
  }
}
